<template>
  <el-row type="flex" class="row-bg" justify="space-between" style="width:100%;">
    <el-col :span="19">
      {{ treeNode.name }}
    </el-col>
    <el-col :span="5">
      <el-row type="flex" class="row-bg" justify="space-between">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <el-dropdown @command="operateDepts">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async operateDepts(command) {
      if (command === 'add') {
        // 这里执行 add 添加方法
        this.$emit('addDept', this.treeNode)
      } else if (command === 'edit') {
        // 这里执行edit  编辑方法
        this.$emit('editDept', this.treeNode)
      } else if (command === 'del') {
        // 这里执行删除方法
        try {
          await this.$confirm('确定要删除吗')
          await delDepartments(this.treeNode.id)
          this.$emit('deldepts')
          this.$message.success('删除成功!')
        } catch (error) {
          this.$message.error('删除失败!')
        }
      }
    }
  }
}
</script>

<style>

</style>
